﻿using RXY_Mall.Application.Shop;
using RXY_Mall.Application.Shop.Dto;
using RXY_Mall.Application.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class CompanyController : BaseController
    {
        private IShopService _shopService;
        private IStatisticsService _statisticsService;
        public CompanyController()
        {
            _shopService = new ShopService();
            _statisticsService = new StatisticsService();
        }

        #region 商家
        public ActionResult List()
        {
            var statistics = _statisticsService.GetCompanyStatistics();
            if (statistics == null)
            {
                ViewBag.user_total = 0;
                ViewBag.user_today = 0;
            }
            else
            {
                ViewBag.user_total = statistics.user_total;
                ViewBag.user_today = statistics.user_today;
            }
            return View();
        }

        public JsonResult GetCompanysByPage(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string companyName, string realName, int status)
        {
            var result = _shopService.GetCompanysByPage(dateMin, dateMax, companyName, realName, status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddCompany(int id = 0)
        {
            ViewBag.id = id;
            ViewBag.text = id == 0 ? "添加" : "修改";
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> AddorEditCompanyInfo(CompanyInput input)
        {
            if (input.id == 0 && _shopService.IsExistCompany(input.company_name))
            {
                return Json(new { status = "n", info = "该公司名称已存在！" });
            }
            else
            {
                var result = await _shopService.AddorEditCompany(MemberId, input);
                if (result.code == 1)
                {
                    return Json(new { status = "y", info = "保存成功。" });
                }
                else
                {
                    return Json(new { status = "n", info = result.msg });
                }
            }
        }

        [HttpGet]
        public JsonResult CompanyInfo(int id = 0)
        {
            if (id > 0)
            {
                var model = _shopService.GetCompanyById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        //禁用/启用商家
        [HttpPost]
        public ActionResult SetComIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _shopService.SetIsComUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        #endregion

        #region 店铺
        public ActionResult Store()
        {
            return View();
        }
        public JsonResult StoreList(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string shopName, string managerName, int status)
        {
            int totalCount = 0;
            var list = _shopService.GetShopsByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, shopName, managerName, status, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用店铺
        [HttpPost]
        public ActionResult SetShopIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _shopService.SetIsShopUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        public ActionResult AddStore(int id = 0)
        {
            ViewBag.id = id;
            return View();
        }
        public ActionResult EditStore(int id = 0)
        {
            ViewBag.id = id;
            return View();
        }
        [HttpGet]
        public JsonResult StoreInfo(int id = 0)
        {
            if (id > 0)
            {
                var model = _shopService.GetShopById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }
        [HttpPost]
        public async Task<JsonResult> AddorEditShopInfo(ShopInput input)
        {
            if (input.id == 0 && _shopService.IsExistShop(input.name))
            {
                return Json(new { status = "n", info = "该店铺名称已存在！" });
            }
            else
            {
                var result = await _shopService.AddorEditShop(MemberId, input);
                if (result.code == 1)
                {
                    return Json(new { status = "y", info = result.msg, sId = result.data });
                }
                else
                {
                    return Json(new { status = "n", info = result.msg });
                }
            }
        }

        //设置店铺类别
        public ActionResult SetStoreCategory(int id = 0)
        {
            ViewBag.id = id;
            ViewBag.name = id;
            if (id > 0)
            {
                var model = _shopService.GetShopById(id);
                if (model != null)
                {
                    ViewBag.name = model.name;
                }
            }
            return View();
        }

        [HttpGet]
        public JsonResult shopCategoryList(int id = 0)
        {
            if (id > 0)
            {
                var list = _shopService.GetShopCategoryList(id);
                return Json(list, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        [HttpPost]
        public async Task<JsonResult> SaveshopCategory(int id, List<ShopCategoryInput> addList = null, List<ShopCategoryInput> delList = null)
        {
            var result = await _shopService.SetShopCategory(MemberId, id, addList, delList);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        #endregion

        #region 添加商品
        public ActionResult SelectGoodsCategory(int id = 0)
        {
            ViewBag.id = id;
            var list = _shopService.GetShopCategoryList(id);
            return View(list);
        }
        public ActionResult AddGoods(int id = 0, int tid = 0)
        {
            int type = Request.QueryString["type"] == null ? 0 : int.Parse(Request.QueryString["type"]);
            ShopCategoryDto model = new ShopCategoryDto();
            if (id > 0 && tid > 0)
            {
                model = _shopService.GetShopCategory(id, tid, type);
            }
            ViewBag.NowTime = DateTime.Now.ToString("yyyy-MM-dd");
            return View(model);
        }
        #endregion
    }
}